/**
 * RenweiWu
 * PubDriver.java
 * 2017年9月19日
 * PubDriver
 * TODO
 * 
 */
package com.etrans.web.gps.business.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.PubDriver;
import com.etrans.web.gps.business.base.domain.PubDrivervo;
import com.etrans.web.gps.business.base.service.PubDriverService;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.WebServiceUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;


/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年9月19日
 * @ClassName:  PubDriver  
 * @param:   
 */
@Controller
@RequestMapping("/base/PubDriver/")
public class PubDriverController extends BaseController{
	
	@Autowired
	private PubDriverService pubDriverService;
	@Autowired
	private CommonService commonService;
	@Autowired  
    private Config config; //引用统一的参数配置类  
	
	
	@RequestMapping("toPubDriver.html")
	public String toPubDriver(Model model,String licence){
		model.addAttribute("licence",licence);
		model.addAttribute("imgPath",config.imagePath);
		return "views/business/base/pubdriver/pubdriver";
	}
	
	@RequestMapping("selectVehicleList.html")
	@ResponseBody
	public List<HashMap<String,Object>> selectVehicleList(){
		int areaId = this.getSysUser().getArea_id();
		HashMap<String,Object> map = getReqParameters();
		map.put("selected", "selected");
		map.put("fullID", String.format("%08d",areaId));
		return commonService.queryVeListByRole(map);
	}
	
	@RequestMapping("queryOrganzation.html")
	public @ResponseBody List<HashMap<String,Object>> querOrganzation(String id){
		return pubDriverService.queryOrganzation(id);
	}
	
	@RequestMapping("queryPubDriver.html")
	public @ResponseBody PageVo queryPubDriver(String paramsGrid,String licence){
		Map<String,Object> map = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		int areaId = this.getSysUser().getArea_id();
		map.put("FullID", String.format("%08d",areaId));
		if(licence==null||"".equals(licence)){
			licence=((String)map.get("licence"));
		}
		map.put("licence",licence);
		map.put("startpage",((int)map.get("page")-1)*((int)map.get("pageSize")));
		map.put("limit",(int)map.get("pageSize"));
		return pubDriverService.queryPubDriver(map);
	}
	
	@RequestMapping(value="toaddPubDriver.html")
	public String toaddPubDriver(Model modle,@RequestParam(value="id",required=false) String id){
		if(id!=null&&!"".equals(id)){
			PubDriver vo =pubDriverService.queryOneById(Integer.parseInt(id));
			modle.addAttribute("vo",vo);
			modle.addAttribute("action","update");
		}else{
			modle.addAttribute("action","save");
		}
		modle.addAttribute("imgPath",config.imagePath);
		return "views/business/base/pubdriver/addpubdriver";
	}


	
	@RequestMapping("addPubDriver.html")
	public	@ResponseBody Map<String,Object> addPubDriver(PubDrivervo vo){
		int code =1;
		String msg="从业人员添加失败";
		if(pubDriverService.addpubdriver(vo))
			code = 0;
			msg="从业人员添加成功";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code",code);
		map.put("msg",msg);
		return map;			
	}
	
	@RequestMapping("updatePubDriver.html")
	public	@ResponseBody Map<String,Object> updatePubDriver(PubDrivervo vo){
		int code =1;
		if(pubDriverService.updatePubDriver(vo))
			code = 0;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code",code);
		return map;			
	}
	
	
	@RequestMapping("deleteDriver.html")
	public  @ResponseBody Map<String, Object> deleteDriver(String id){
		Map<String,Object> map =new HashMap<String,Object>();
		if(id!=null&&!"".equals(id)){
			String[] sid = id.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for (String str : sid) {
				list.add(Integer.parseInt(str));
			}
			if(pubDriverService.deleteDriver(list)){
				map.put("code", 0);
			}else {
				map.put("code", 1);
			};
		}else {
			map.put("code", 1);
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportDriver.html")
	public void exportRecord( HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid, String colModel, String title, String StartPage, String EndPage, String pagesize) {
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("startpage",Integer.parseInt(StartPage)-1);
		param.put("limit",(Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
		paramMap.putAll(param);
		int areaId = this.getSysUser().getArea_id();
		paramMap.put("FullID", String.format("%08d",areaId));
		PageVo pageVo = pubDriverService.queryPubDriver(paramMap);
		List<PubDriver> list = (List<PubDriver>)pageVo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(list.size()-1)){
			end=list.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			PubDriver obj=(PubDriver)list.get(i);
			map.put("name", obj.getName()==null ? "" : obj.getName());
			map.put("enprise_name", obj.getEnprise_name()==null ? "" : obj.getEnprise_name());
			map.put("id_card", obj.getId_card()==null ? "" : obj.getId_card());
			map.put("driver_licence", obj.getDriver_licence()==null?"":obj.getDriver_licence());
			String drive_type="";
			switch(obj.getDrive_type()){
				case 1 : drive_type="客车";
					break;
				case 2 : drive_type="危险品";
					break;
				case 3 : drive_type="货运车";
					break;
				default : drive_type="";
			}
			map.put("drive_type", drive_type);
			map.put("vehicle_no", obj.getVehicle_no()==null ? "" : obj.getVehicle_no());
			map.put("lssuing_auth_name", obj.getLssuing_auth_name()==null ? "" : obj.getLssuing_auth_name());
			map.put("licence", obj.getLicence()==null ? "" : obj.getLicence());
			map.put("acquire_time", obj.getAcquire_time()==null ? "" : DateUtil.getDateTimeString(obj.getAcquire_time(),null));
			map.put("invalid_start_time", obj.getInvalid_start_time()==null ? "" : DateUtil.getDateTimeString(obj.getInvalid_start_time(),null));
			map.put("invalid_end_time", obj.getInvalid_end_time()==null ? "" : DateUtil.getDateTimeString(obj.getInvalid_end_time(),null));
			map.put("status", obj.getStatus()==0 ? "注销":obj.getStatus()==10 ? "从业":"行业");
			map.put("phone", obj.getPhone()==null ? "" :obj.getPhone());
			map.put("enable_sms", obj.getEnable_sms()==1 ? "启用" :"停用");
			map.put("img_url", obj.getImg_url()==null ? "" :obj.getImg_url());
			map.put("has_sync", obj.getHas_sync()==1 ? "已同步" :"未同步");
			rows.add(map);
		}
		
		int index=-1;
		@SuppressWarnings("rawtypes")
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
	
	// 获取从业人员信息
	@RequestMapping(value = "getDriverInfo.html")
	@ResponseBody
	public Map<String, Object> getDriverInfo(@RequestParam String xm, String sfzh,String ziparea) {
		Map<String, Object> result = new HashMap<>();
		try {
			result =WebServiceUtil.getDriverInfoXml(xm,sfzh,ziparea);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", Constant.RETURN_ERROR_CODE);
			result.put("msg", "操作失败!");
		}
		return result;
	}
	
	// 同步运政从业人员信息
	@RequestMapping(value = "synDriverInfo.html")
	@ResponseBody
	public Map<String, Object> synDriverInfo(String[] vehicleIds) {
		Map<String, Object> result = new HashMap<>();
		try {
			for (int i = 0; i < vehicleIds.length; i++) {
				Map<String,Object> mo = pubDriverService.queryDriverVoById(Integer.valueOf(vehicleIds[i]));
				if(mo.get("name")!=null&&mo.get("idcard")!=null&&mo.get("code")!=null){
					boolean flag = pubDriverService.synDriverInfo(mo.get("name").toString(),mo.get("idcard").toString(),mo.get("code").toString());
					if(flag){
						result.put("code", Constant.RETURN_OK_CODE);
						result.put("msg", "操作成功!");
					}else{
						result.put("code", 1);
						result.put("msg", "运政无返回数据!");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", Constant.RETURN_ERROR_CODE);
			result.put("msg", "操作失败!");
		}
		return result;
	}
}
